package org.smr.ministore.controller;

import javax.annotation.Resource;

import org.blade.msg.DataMessage;
import org.blade.msg.ErrorCode;
import org.blade.msg.Message;
import org.blade.utils.Pager;
import org.smr.ministore.entities.Goods;
import org.smr.ministore.entities.vo.Assign2OrganVO;
import org.smr.ministore.entities.vo.Assign2OrgansVO;
import org.smr.ministore.service.GoodsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestBody;


@Controller("GoodsController")
public class GoodsController {

    @Resource(name = "goodsServiceImpl")
    private GoodsService service;

    /**
     * 保存编辑后的Goods信息
     *
     * @param Goods
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.PATCH, value = "/Goods/edit")
    public Message modify(@RequestBody Goods Goods) {
        Message msg = new Message();

        if (null == Goods.getId()) {
            msg.setStatus(service.save(Goods));
        } else {
            msg.setStatus(service.updateById(Goods));
        }
        return msg;
    }

    /**
     * 跳转到列表查询Goods
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/Goods/list-page")
    public String toList() {
        return "/Goods/Goods-list";
    }

    /**
     * 根据id查询Goods
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/Goods")
    public Goods get(Long id) {
        return service.queryById(id);
    }


    /**
     * 分页查询Goods信息
     *
     * @param Goods
     * @param pageSize
     * @param pageNum
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/Goods/list")
    public Pager<Goods> list(@RequestBody Goods Goods, long pageSize, long pageNum) {

        return service.paginate(Goods, pageSize, pageNum);

    }


    /**
     * 根据ID删除Goods信息
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/Goods/remove", method = RequestMethod.DELETE)
    public Message remove(Long id) {

        Message msg = new Message();
        msg.setStatus(service.deleteById(id));
        return msg;
    }

    /**
     * 根据ID删除Goods信息
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/Goods/removeByLogic", method = RequestMethod.DELETE)
    public Message removeByLogic(Long id) {

        Message msg = new Message();
        msg.setStatus(service.deleteByIdLogical(id));
        return msg;
    }


    @ResponseBody
    @RequestMapping(value = "/Goods/assign2Organs", method = RequestMethod.POST)
    public Message assign2Organs(@RequestBody Assign2OrgansVO assign2Organs) {
        Message msg = new Message();
        msg.setStatus(service.assign(assign2Organs));
        return msg;
    }

    @ResponseBody
    @RequestMapping(value = "/Goods/assign2Organ", method = RequestMethod.POST)
    public Message assign2Organ(@RequestBody Assign2OrganVO assign2Organ) {
        Message msg = new Message();
        msg.setStatus(service.assign(assign2Organ));
        return msg;
    }

    @ResponseBody
    @RequestMapping(value = "/Goods/queryGoodsAssignee", method = RequestMethod.POST)
    public Message queryGoodsAssignee(@RequestBody Assign2OrgansVO assign2Organs) {
        DataMessage msg = new DataMessage(
                ErrorCode.Success.SUCCESS, "",
                service.queryGoodsAssignee(assign2Organs));
        return msg;
    }


}
